package com.ml.mall.api.ipay;

import com.ml.mall.bean.req.ipay.Ipay88CallBackDto;
import com.ml.mall.consts.CommonConsts;
import com.ml.mall.consts.PermissionConst;
import com.ml.mall.entity.detail.SysOrderDetail;
import com.ml.mall.entity.order.SysOrder;
import com.ml.mall.entity.sys.SysUser;
import com.ml.mall.security.JwtUtil;
import com.ml.mall.service.asyn.OrderMailServiceImplAsyn;
import com.ml.mall.service.detail.SysOrderDetailService;
import com.ml.mall.service.ipay.Ipay88Service;
import com.ml.mall.service.order.impl.SysOrderServiceImpl;
import com.ml.mall.service.supplier.SysOrderSupplierService;
import com.ml.mall.service.supplier.impl.SysOrderSupplierServiceImpl;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.security.NoSuchAlgorithmException;
import java.util.List;

/**
 * @description description
 * @author yx
 * @date 2021/9/22
 * @version 1.0
*/
@Api("【order】支付回调")
@RestController
@Slf4j
@RequestMapping("/ipay88")
public class Ipay88Controller {
    @Value("${dns.front_url}")
    private String frontUrl;

    @Autowired
    private Ipay88Service ipay88Service;

    @Autowired
    private SysOrderSupplierServiceImpl sysOrderSupplierService;

    @Autowired
    private SysOrderServiceImpl sysOrderService;

    @Autowired
    private SysOrderDetailService sysOrderDetailService;
    @PostMapping("/callback")
//    @RequiresPermissions(PermissionConst.IPAY88_CALLBACK) //放行，不启用
    public String ipay88CallBack(@Valid Ipay88CallBackDto dto) throws NoSuchAlgorithmException {
        log.info("callback====={}", dto);
        ipay88Service.receiveCallBack(dto);
        if(dto.getStatus().equals(CommonConsts.IPAY88_PAYMENT_SUCCESSFUL)){
            //异步发送邮件
            //发送卖家订单邮件
            sysOrderSupplierService.sendSuppliersOrderEmail(Long.parseLong(dto.getRefNo()));
            //发送买家订单邮件
            SysUser currentUser = JwtUtil.getCurrentUser();
            SysOrder sysOrder = sysOrderService.selectOrderByOid(Long.parseLong(dto.getRefNo()));
            List<SysOrderDetail> listByOid = sysOrderDetailService.getListByOid(Long.parseLong(dto.getRefNo()));
            sysOrderService.SendOrderEmail(sysOrder,listByOid,currentUser);
        }
        return "RECEIVEOK";
    }

    @PostMapping("/responseUrl")
//    @RequiresPermissions(PermissionConst.IPAY88_RESPONSE_URL) //放行，不启用
    public void ipay88ReposenUrl(@Valid Ipay88CallBackDto dto, HttpServletResponse response) throws Exception{
        String paymentSuccessfulUrl = frontUrl + "/Order-Result?state=1&viewType=4&oid=" + dto.getRefNo();
        String paymentFailedUrl = frontUrl + "/Order-Result?state=0&viewType=4&oid=" + dto.getRefNo();

        log.info("response====={}",dto);
        ipay88Service.receiveCallBack(dto);
        //根据支付状态进行跳转
        String redirectUrl = "";
        //TODO: 支付成功和失败后的跳转地址暂时设置为baidu和sogou,跳转页面确定后再写入配置文件
        switch (dto.getStatus()){
            case CommonConsts.IPAY88_PAYMENT_FAILED: redirectUrl = paymentFailedUrl; break;
            case CommonConsts.IPAY88_PAYMENT_SUCCESSFUL:
            case CommonConsts.IPAY88_PAYMENT_PADDING:  //待定与成功都使用一个
                redirectUrl = paymentSuccessfulUrl; break;
        }
        log.info("redirectTo====={}", redirectUrl);
        response.sendRedirect(redirectUrl);
    }
}
